// lifeservice/iqtest/index.js
import iq from '../../idata/iq.js'
var timer;
var timer1;
const innerAudioContext = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    min: 30,
    sec: 0,
    sid: 0,
    total: 0,
    score: [
      { '>140': '天才' },
      { '100-140': '优秀' },
      { '70-100': '正常' },
      { '40-70': '有点傻' },
      { '<40': '白痴' }
    ],
    flag: true,
    flag1: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('isAnswer')) {
      this.setData({
        flag: false,
        flag1: true,
        imgs: [1, 1, 1]
      })
    } else {
      this.setData({
        flag: true,
        flag1: false,
        imgs: [1, 1, 1]
      })
    }
    console.log(iq.iq);
    var sid = this.data.sid;
    this.setData({
      iqf: iq.iq[sid],
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  beginAnswerQuestion() {
    var min = this.data.min;
    var sec = this.data.sec;
    this.setData({
      flag1: true,
    })
    clearInterval(timer1);
    timer1 = setInterval(function () {
      if (min >= 0 && sec > 0) {
        sec--;
      } else if (min > 0 && sec == 0) {
        min--;
        sec = 59;
      } else if (min == 0 && sec == 0) {
        clearInterval(timer1);
        var total = this.data.total
        this.showTotal(total)
      }
      this.setData({
        min: min,
        sec: sec
      })
    }.bind(this), 1000)

  },
  getMyAnswer(e) {
    var anw = e.currentTarget.dataset.anw;
    var req = e.currentTarget.dataset.req;
    var total = this.data.total;
    var sid = this.data.sid;
    console.log(anw);
    console.log(req);
    if (anw == req) {
      total = total + 5;
    } else {
      total = total;
    }
    sid++;
    if (sid < 30) {
      this.showAnimation();
      clearTimeout(timer);
      timer = setTimeout(function () {
        this.setData({
          total: total,
          sid: sid,
          iqf: iq.iq[sid],
          val: '',
        })
      }.bind(this), 1000)

    } else {
      console.log('total', total)
      this.setData({
        total: total,
      })
      this.showTotal(total);
    }

  },
  showTotal(total) {
    console.log('没有执行我')
    console.log(total);
    this.setData({
      flag: false
    })
    if (total >= 100) {
      innerAudioContext.autoplay = true;
      innerAudioContext.src = 'http://pe76dvko7.bkt.clouddn.com/iq1.mp3';
      if (total >= 140) {
        this.setData({
          imgs: [1, 1, 1, 1, 1]
        })
      } else if (total >= 100 && total < 140) {
        this.setData({
          imgs: [1, 1, 1, 1]
        })
      } else if (total >= 70 && total < 100) {
        this.setData({
          imgs: [1, 1, 1]
        })
      }
    } else {
      innerAudioContext.autoplay = true;
      innerAudioContext.src = 'http://pe76dvko7.bkt.clouddn.com/iq2.mp3';
      if (total >= 40 && total < 70) {
        this.setData({
          imgs: [1, 1],
        })
      } else if (total < 40) {
        this.setData({
          imgs: [1],
        })
      }
    }
    wx.setStorageSync('isAnswer', true)
  },
  showAnimation() {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
    this.animation = animation
    console.log(11111);
    this.animation.translateX(30).opacity(0).step()
    this.animation.translateX(0).opacity(1).step()
    this.setData({
      animationData: this.animation.export()
    })
  },
  getUserInput(e) {
    console.log(e)
    var val = e.detail.value;
    this.setData({
      val: val
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res);
    if (res.from == "button") {
      wx.setStorageSync('isAnswer', false);
      clearInterval(timer1);
      this.setData({
        flag: true,
        sid: 0,
        total: 0,
        min: 30,
        sec: 0,
        flag: true,
        flag1: false,
      })
      this.onLoad();

      return {
        title: '',
        desc: '',
        path: '/pages/iqtest/index',
        success: res => {
          console.log(res)
        },
        fail: err => {
          console.log(err)
        },
        complete: res => {
          console.log(res)
        }
      }
    }
  }
})